package org.wuzhou.lvyou.util;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class GPSHelper {
	private Context context;//声明上下文环境
	private double mLatitude=0;//纬度
	private double mLongitude=0;//经度
	private LocationManager mLocationManager;//位置管理对象
	
	public GPSHelper(Context context){
		this.context=context;
	}
	
	public static GPSHelper mGps;
	//获取GPS对象实例
	public static GPSHelper getGpsInstance(Context mContext){
		if(mGps==null){
			mGps=new GPSHelper(mContext);
		}
		return mGps;
	}
	
	//
	public void openGps(){
		mLocationManager=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
		mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,6000,0,locationListener);
	}
	
	//
	public void closeGps() {
		if (mLocationManager != null) {
			mLocationManager.removeUpdates(locationListener);
		}
	}
	
	//声明位置监听实例
	private final LocationListener locationListener=new LocationListener(){

		@Override
		public void onLocationChanged(Location location) {
			updateWithNewLocation(location);
		}

		@Override
		public void onProviderDisabled(String provider) {
		}

		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
		
	};
	/**
	 * 
	 * @param location
	 */
	protected void updateWithNewLocation(Location location) {
		if(location!=null){
			mLatitude=location.getLatitude();
			mLongitude=location.getLongitude();
		}
		
	}
	

	/**
	 * @return the mLatitude
	 */
	public double getmLatitude() {
		return mLatitude;
	}

	/**
	 * @param mLatitude the mLatitude to set
	 */
	public void setmLatitude(double mLatitude) {
		this.mLatitude = mLatitude;
	}

	/**
	 * @return the mLongitude
	 */
	public double getmLongitude() {
		return mLongitude;
	}

	/**
	 * @param mLongitude the mLongitude to set
	 */
	public void setmLongitude(double mLongitude) {
		this.mLongitude = mLongitude;
	}
	
	
}
